package cn.lyjuan.dwz_manage.core.java.action;


import cn.lyjuan.dwz_manage.core.base.pojo.ManagePojo;
import cn.lyjuan.dwz_manage.core.base.pojo.RolePojo;
import cn.lyjuan.dwz_manage.core.java.dto.QueryResultBean;
import cn.lyjuan.dwz_manage.core.java.service.ManageService;
import cn.lyjuan.dwz_manage.core.java.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * Created by ly on 2015/1/16.
 */
@Controller
@RequestMapping("manage/")
public class ManageAction extends BaseAction
{
    @Autowired
    @Qualifier("mgManageService")
    private ManageService manageSer;

    @Autowired
    @Qualifier("mgRoleService")
    private RoleService roleSer;

    @RequestMapping("manageList")
    public void manageList(HttpServletRequest req)
    {
        ManagePojo manage = manageSer.queryManageByName(this.getUserName());

        Map<String, String> param = this.paramsToMap();

        QueryResultBean<ManagePojo> bean = manageSer.queryManages(manage, param, this.getDwzPn(), this.getDwzPs());

        this.pkgManagePage(bean.getTotalCount());
        req.setAttribute("manages", bean.getList());
    }

    @RequestMapping("addManage")
    @ResponseBody
    public void addManage(HttpServletRequest req, HttpServletResponse resp, ManagePojo manage)
    {
        ManagePojo creater = manageSer.queryManageByName(this.getUserName());

        manage = manageSer.addManage(creater, manage);

        this.returnDwzAjaxSucc();
    }

    @RequestMapping("preUpdateManage")
    public void preUpdateManage(String name, HttpServletRequest req)
    {
        ManagePojo manage = manageSer.queryManageByName(name);

        req.setAttribute("manage", manage);
    }

    @RequestMapping("updateManage")
    @ResponseBody
    public void updateManage(ManagePojo toManage)
    {
        ManagePojo fromManage = manageSer.queryManageByName(toManage.getName());

        manageSer.saveManage(fromManage, toManage);

        this.returnDwzAjaxSucc();
    }

    /**
     * 查看用户的角色
     * @return
     */
    @RequestMapping("preRelateRole")
    public void preRelateRole(String name, Model model)
    {
        // 当前用户
        ManagePojo manage = manageSer.queryManageByName(this.getUserName());

        // 被操作用户
        ManagePojo user = manageSer.queryManageByName(name);

        // 查询有权限的所有角色
        List<RolePojo> list = roleSer.queryRolesForManage(manage, null, 1, Integer.MAX_VALUE).getList();

        // 查询被操作用户已有的角色
        List<RolePojo> userRoles = roleSer.queryRelatedRole(user);

        model.addAttribute("user", user);
        model.addAttribute("roles", list);
        model.addAttribute("userRoles", userRoles);
    }

    /**
     * 给用户关联角色
     * @param name      被操作管理员的账号
     * @param roleIds
     */
    @RequestMapping("relateRole")
    @ResponseBody
    public void relateRole(String name, Integer[] roleIds)
    {
        // 当前用户
        ManagePojo manage = manageSer.queryManageByName(this.getUserName());
        // 当前用户有权限的角色
        List<RolePojo> hadRoles = roleSer.queryRolesForManage(manage, null, 1, Integer.MAX_VALUE).getList();

        // 被操作用户
        ManagePojo user = manageSer.queryManageByName(name);

        // 关联角色
        manageSer.relateRole(manage, hadRoles, user, roleIds);

        this.returnDwzAjaxSucc();
    }
}

